Page({
  data: {
    activeCategory: '1',
    searchKeyword: '',
    isSearching: false,
    showNoResult: false,
    categories: [
      { id: '1', name: '半成品'},
      { id: '2', name: '成品' },
      { id: '3', name: '酱料' },
      { id: '4', name: '馅料' },
      { id: '5', name: '粉类' },
      { id: '6', name: '油类'},
      { id: '7', name: '冻品' },
      { id: '8', name: '调味料'}
    ],
    allProducts: [
      { 
        id: '1', 
        name: '维嘟香蕉飞饼', 
        code: '编码:xjfb..80', 
        price: '500', 
        category: '1',
        image: '/images/food/product1.jpg'
      },
      { 
        id: '2', 
        name: '维嘟香葱豆沙饼', 
        code: '编码:xcfb..80', 
        price: '500', 
        category: '1',
        image: '/images/food/product2.jpg'
      },
      { 
        id: '3', 
        name: '蚝萃轩黄油酥筒', 
        code: '编码:hyst..80', 
        price: '360', 
        category: '1',
        image: '/images/food/product3.jpg'
      },
      { 
        id: '4', 
        name: '牛角包', 
        code: '编码:llfb..80', 
        price: '600', 
        category: '1',
        image: '/images/food/product4.jpg'
      },
      { 
        id: '5', 
        name: '漫竹沙华广式油条', 
        code: '编码:MZSHGSYT100', 
        price: '280', 
        category: '2',
        image: '/images/food/product5.jpg'
      },
      { 
        id: '6', 
        name: '金轮高筒蛋挞皮', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '2',
        image: '/images/food/product6.jpg'
      },
      { 
        id: '7', 
        name: '老干妈', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '2',
        image: '/images/food/product7.jpg'
      }
      ,
      { 
        id: '8', 
        name: '酸辣粉', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '3',
        image: '/images/food/product8.jpg'
      },
      { 
        id: '9', 
        name: '酸辣粉', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '4',
        image: '/images/food/product9.jpg'
      },
      { 
        id: '10', 
        name: '酸辣粉', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '5',
        image: '/images/food/product10.jpg'
      },
      { 
        id: '11', 
        name: '酸辣粉', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '6',
        image: '/images/food/product11.jpg'
      },
      { 
        id: '12', 
        name: '酸辣粉', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '7',
        image: '/images/food/product12.jpg'
      },
      { 
        id: '13', 
        name: '酸辣粉', 
        code: '编码:Z12...00100', 
        price: '100', 
        category: '8',
        image: '/images/food/product13.jpg'
      }
    ],
    filteredProducts: [],
    displayProducts: []
  },
  
  onLoad: function() {
    this.initData();
  },
  
  initData: function() {
    // 初始化数据
    this.filterProducts('1');
  },
  
  // 切换分类
  switchCategory: function(e) {
    const categoryId = e.currentTarget.dataset.id;
    this.setData({
      activeCategory: categoryId,
      searchKeyword: '',
      isSearching: false,
      showNoResult: false
    });
    this.filterProducts(categoryId);
  },
  
  // 根据分类筛选商品
  filterProducts: function(categoryId) {
    const filtered = this.data.allProducts.filter(item => item.category === categoryId);
    this.setData({
      filteredProducts: filtered,
      displayProducts: filtered,
      showNoResult: filtered.length === 0
    });
  },
  
  // 搜索输入处理
  handleSearchInput: function(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    
  
  },
  
  // 执行搜索
  handleSearchConfirm: function() {
    const keyword = this.data.searchKeyword.trim();
    if (!keyword) {
      wx.showToast({
        title: '请输入搜索关键词',
        icon: 'none'
      });
      return;
    }
    
    this.doSearch(keyword);
  },
  
  // 实际搜索逻辑
  doSearch: function(keyword) {
    const results = this.data.allProducts.filter(item => {
      return item.name.includes(keyword) || 
             item.code.includes(keyword);
    });
    
    this.setData({
      displayProducts: results,
      isSearching: true,
      showNoResult: results.length === 0
    });
  },
  
  // 清除搜索
  clearSearch: function() {
    this.setData({
      searchKeyword: '',
      isSearching: false,
      displayProducts: this.data.filteredProducts,
      showNoResult: false
    });
  },
  
  // 跳转到商品详情
  navigateToDetail: function(e) {
    const productId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + productId
    });
  },
  
  navigateToDetail: function(e) {
    const productId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + productId  // 跳转到详情页，携带商品ID
    });
  },
});